package com.bilibili.bilibili.activity;

import android.content.res.Configuration;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.VideoView;

import com.android.volley.VolleyError;
import com.bilibili.bilibili.R;
import com.bilibili.bilibili.model.CommendDetailsModel;
import com.bilibili.bilibili.network.HttpGetData;
import com.bilibili.bilibili.network.VolleyInterface;
import com.bilibili.bilibili.utils.GsonUtil;
import com.bilibili.bilibili.utils.URLUtils;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import master.flame.danmaku.controller.DrawHandler;
import master.flame.danmaku.danmaku.model.BaseDanmaku;
import master.flame.danmaku.danmaku.model.DanmakuTimer;
import master.flame.danmaku.danmaku.model.IDanmakus;
import master.flame.danmaku.danmaku.model.android.DanmakuContext;
import master.flame.danmaku.danmaku.model.android.Danmakus;
import master.flame.danmaku.danmaku.parser.BaseDanmakuParser;
import master.flame.danmaku.ui.widget.DanmakuView;

public class CommendDetails extends AppCompatActivity {
    private DanmakuView danmakuView;
    private VideoView videoView;
    private boolean showdanmu;
    private DanmakuContext danmakuContext;
    private BaseDanmakuParser parser = new BaseDanmakuParser() {
        @Override
        protected IDanmakus parse() {
            return new Danmakus();
        }
    };
    private long currentTime;
    private ImageView imageView;
    private LinearLayout layout;
    private Button btn_send;
    private EditText edit_text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_commend_details);
 /*       View decorView = getWindow().getDecorView();
        int fullscreen = View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(fullscreen);*/
       ActionBar actionBar=getSupportActionBar();
        //actionBar.hide();
        initView();
        getData();
    }

    private void initView() {
        edit_text = (EditText) findViewById(R.id.edit_text);
        btn_send = (Button) findViewById(R.id.send);
        layout = (LinearLayout) findViewById(R.id.operation_layout);
        imageView = (ImageView) findViewById(R.id.commend_details_imageview);
        danmakuView = (DanmakuView) findViewById(R.id.recommend_danmu);
        videoView = (VideoView) findViewById(R.id.recommend_videoview);
        danmakuView.enableDanmakuDrawingCache(true);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                videoView.start();
            }
        });

    }

    private void getData() {
        HttpGetData.RequestGet(CommendDetails.this, URLUtils.COMMEND_DETAILS, "Djn", new VolleyInterface(CommendDetails.this,VolleyInterface.mListener,VolleyInterface.mErrorListener) {
            @Override
            public void onMySuccess(String result) {
                CommendDetailsModel model = GsonUtil.parseJsonToObj(result, new TypeToken<CommendDetailsModel>(){});
                String url = model.getDurl().get(0).getUrl();
                MediaController mediaController=new MediaController(CommendDetails.this);
                videoView.setMediaController(mediaController);
                videoView.setVideoURI(Uri.parse(url));
                videoView.start();
                danmakuView.setCallback(new DrawHandler.Callback() {
                    @Override
                    public void prepared() {
                        showdanmu=true;
                        danmakuView.start();
                        generateSomeDanmu();
                    }

                    @Override
                    public void updateTimer(DanmakuTimer timer) {

                    }

                    @Override
                    public void danmakuShown(BaseDanmaku danmaku) {

                    }

                    @Override
                    public void drawingFinished() {

                    }
                });
            }

            @Override
            public void onMyError(VolleyError error) {

            }
        });
        danmakuContext = DanmakuContext.create();
        danmakuView.prepare(parser, danmakuContext);
        danmakuView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (layout.getVisibility() == View.GONE) {
                    layout.setVisibility(View.VISIBLE);
                } else {
                    layout.setVisibility(View.GONE);
                }
            }
        });
        btn_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s = edit_text.getText().toString();
                addDanmu(s, true);//添加到弹幕里面
                edit_text.setText("");//发送后将输入框为空
            }
        });

    }

    //添加弹幕
    private void addDanmu(String content, boolean flag) {
        BaseDanmaku danmaku = danmakuContext.mDanmakuFactory.createDanmaku(BaseDanmaku.TYPE_SCROLL_RL);
        danmaku.text = content;
        danmaku.textSize = 25;//字体大小
        danmaku.padding = 50;//设置弹幕间隔距离
        danmaku.textColor = Color.RED;//设置弹幕的颜色
        danmaku.setTime(danmakuView.getCurrentTime());
        if (flag) {
            danmaku.textColor = Color.GREEN;
        }
        danmakuView.addDanmaku(danmaku);
    }

    private void generateSomeDanmu() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (showdanmu) {
                    List<String> list = new ArrayList<String>();
                    list.add("666");
                    list.add("很牛逼");
                    list.add("张贤伟");
                    list.add("丁江楠");
                    list.add("郭传培");
                    list.add("冯洁");
                    addDanmu(list.toString(), false);
                    try {
                        Thread.sleep(800);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (danmakuView != null && danmakuView.isPrepared()) {
            danmakuView.pause();
            currentTime = danmakuView.getCurrentTime();
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        danmakuView.start();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (danmakuView != null) {
            danmakuView.resume();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        showdanmu = false;
        if (danmakuView != null) {
            danmakuView.release();
            danmakuView = null;
        }
    }

    public void onConfigurationChanged(Configuration newConfig) {
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            Log.e("info", "ºáÆÁ");
            layout.setVisibility(View.GONE);
        } else {
            Log.e("info", "ÊúÆÁ");
            layout.setVisibility(View.VISIBLE);
        }
        super.onConfigurationChanged(newConfig);
        videoView.refreshDrawableState();

    }
}
